package com.wf.course.web.controller.ad;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.Query;

import org.apache.log4j.varia.FallbackErrorHandler;
import org.aspectj.apache.bcel.generic.ReturnaddressType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.StringUtils;
import com.utils.page.PageBean;
import com.wf.course.model.ad.Coupons;
import com.wf.course.model.ad.MemberCoupons;
import com.wf.course.model.ad.VipCard;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.model.member.Member;
import com.wf.course.service.ICouponsService;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.service.IMemberCouponsService;
import com.wf.course.service.IMemberService;
import com.wf.course.service.IVipCardService;
import com.wf.course.web.Interceptor.LogInfo;

/**
 * 优惠券使用情况
 * @author TT
 *
 */
@Controller
@RequestMapping("MemberCoupons")
public class MemberCouponsController {
	@Autowired
	private IMemberCouponsService memberCouponsService;
	@Autowired
	private IMemberService memberService;
	@Autowired
	private ICouponsService couponsService;
	@Autowired
	private ICourseInfoService courseInfoService;
	@Autowired
	private ICourseChapterService chapterService;
	@Autowired
	private IVipCardService cardService;
	
	@LogInfo("用户优惠券信息列表")
	@RequestMapping("queryMemberCoupon")
	public String queryMemberCoupon(
			  @RequestParam(value="pageNo",required=false) Integer pageNo,
			  @RequestParam(value="pageSize",required=false) Integer pageSize,
			  @RequestParam(value="title",required=false) String title,
			  @RequestParam(value="ftype",required=false) Integer ftype,
			  @RequestParam(value="limits",required=false) Integer limits,
			  @RequestParam(value="userName",required=false) String userName,
			  @RequestParam(value="telephone",required=false) String telephone,
			  @RequestParam(value="coupons_source",required=false) Integer coupons_source,
			  @RequestParam(value="stime",required=false) String stime,
			  @RequestParam(value="etime",required=false) String etime,
			  @RequestParam(value="receivestart",required=false) String receivestart,
			  @RequestParam(value="receivend",required=false) String receivend,
			  @RequestParam(value="status",required=false) Integer status,
			  Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("title", title);
		map.put("ftype", ftype);
		map.put("limits", limits);
		map.put("userName", userName);
		map.put("telephone", telephone);
		map.put("coupons_source", coupons_source);
		map.put("stime", stime);
		map.put("etime", etime);
		map.put("receivestart", receivestart);
		map.put("receivend", receivend);
		map.put("status", status);
		
		PageBean<MemberCoupons> pageBean = new PageBean<MemberCoupons>(pageNo,pageSize,null,map);
		pageBean.setUrl("MemberCoupons/queryMemberCoupon");
		memberCouponsService.queryMemberCouponsList(pageBean);
		model.addAttribute("page",pageBean);
		
		
		return "member/queryMemberCoupon";
	}
	
	@LogInfo("加载优惠券管理列表页面")
	@RequestMapping("MemberCouponsList")
	public String CouponsList(@RequestParam(value="pageNo",required=false) Integer pageNo,
							  @RequestParam(value="pageSize",required=false) Integer pageSize,
							  @RequestParam(value="title",required=false) String title,
							  @RequestParam(value="ftype",required=false) Integer ftype,
							  @RequestParam(value="limits",required=false) Integer limits,
							  @RequestParam(value="userName",required=false) String userName,
							  @RequestParam(value="telephone",required=false) String telephone,
							  @RequestParam(value="stime",required=false) String stime,
							  @RequestParam(value="etime",required=false) String etime,
							  @RequestParam(value="orderCode",required=false) String orderCode,
							  Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		map.put("ftype", ftype);
		map.put("limits", limits);
		map.put("userName", userName);
		map.put("telephone", telephone);
		map.put("stime", stime);
		map.put("etime", etime);
		map.put("orderCode", orderCode);
		
		PageBean<MemberCoupons> pageBean = new PageBean<MemberCoupons>(pageNo,pageSize,null,map);
		pageBean.setUrl("MemberCoupons/MemberCouponsList");
		memberCouponsService.getMemberCouponsList(pageBean);
		model.addAttribute("page",pageBean);
		return "ad/memberCouponsList";
	}
	
	/**
	 * 批量赠送优惠券页面加载
	 * @param pageNo
	 * @param pageSize
	 * @param telephone
	 * @return
	 */
	@LogInfo("批量赠送优惠券页面加载")
	@RequestMapping("giftCoupon")
	public String giftCoupon(Model model){
		//查询出所有的优惠券
		List<Coupons> list = couponsService.getCouponsAllList();
		model.addAttribute("list",list);
		return "member/giftCouponList";
	}
	
	/**
	 * 批量赠送优惠券查询
	 * @param pageNo
	 * @param pageSize
	 * @param telephone
	 * @return
	 */
	@LogInfo("批量赠送优惠券查询")
	@RequestMapping("giftCouponSerach")
	public String giftCouponSerach(
				@RequestParam(value="pageNo",required=false) Integer pageNo,
				@RequestParam(value="pageSize",required=false) Integer pageSize,
				@RequestParam(value="telephone",required=false) String telephone,
				@RequestParam(value="orderBeginTime",required=false) String orderBeginTime,
				@RequestParam(value="orderEndTime",required=false) String orderEndTime,
				@RequestParam(value="orderStartAmount",required=false) BigDecimal orderStartAmount,
				@RequestParam(value="orderEndAmount",required=false) BigDecimal orderEndAmount,
				@RequestParam(value="ddlCoupon",required=false) Integer ddlCoupon,

				/*@RequestParam(value="goodsSort",required=false) Integer goodsSort,
			   @RequestParam(value="courseId",required=false) Integer courseId,
			   @RequestParam(value="chapterId",required=false) Integer chapterId,
			   @RequestParam(value="cardId",required=false) Integer cardId,*/
				Model model
			){
		//查询出所有用户
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("telephone", telephone);
		cons.put("orderBeginTime", orderBeginTime);
		cons.put("orderEndTime", orderEndTime);
		cons.put("orderStartAmount", orderStartAmount);
		cons.put("orderEndAmount", orderEndAmount);
		cons.put("ddlCoupon", ddlCoupon);
		
		/*Integer id =0;
		 if(courseId !=null && goodsSort == 1 ){
			 id = courseId;
		 }else if(chapterId!=null && goodsSort==2  ){ 
			 id = chapterId;
		 }else if (cardId != null && goodsSort==3  ) {
			 id=cardId;
		}else {
			id=0;
		}
		 cons.put("goodsSort", goodsSort);
		 cons.put("cid",id);*/
		 
		 
		/*Integer conditionCount = 0;
		if (!StringUtils.isNullOrEmpty(orderBeginTime)) {
			conditionCount ++;
		}
		if (!StringUtils.isNullOrEmpty(orderEndTime)) {
			conditionCount ++;
		}
		if (orderStartAmount != null &&orderStartAmount > 0) {
			conditionCount ++;
		}
		if (orderEndAmount != null && orderEndAmount > 0) {
			conditionCount ++;
		}*/
		/*if (goodsSort !=null && goodsSort >0) {
			conditionCount ++;
		}
		if (id > 0) {
			conditionCount ++;
		}*/
		/*cons.put("conditionCount", conditionCount);*/
		
		
		PageBean<Member> pageBean = new PageBean<Member>(pageNo, pageSize, null, cons);
		pageBean.setUrl("MemberCoupons/giftCouponSerach");
		
		this.memberService.queryAllowGiftMember(pageBean);
		model.addAttribute("page", pageBean);
		
		//查询出所有的优惠券
		List<Coupons> list = couponsService.getCouponsAllList();
		model.addAttribute("list",list);
		
	/*	//课程下拉框
		List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		 //章节下拉框
		 List<CourseChapter> chapters = chapterService.queryCourseChapters();
		 model.addAttribute("chapters",chapters);
		 //年卡下拉框
		 List<VipCard> cards = cardService.queryVipCards();
		 model.addAttribute("cards",cards);*/
		 
		return "member/giftCouponList";
	}
	
	/**
	 * 批量送优惠券
	 * @param id	优惠券id
	 * @param userId	用户userId数组
	 * @return
	 */
	@ResponseBody
	@LogInfo("批量送优惠券")
	@RequestMapping(value="gitf",method=RequestMethod.POST,produces = "text/html;charset=UTF-8")
	public String gitf(@RequestParam("id") Integer id,
					   @RequestParam("userId") String[] userId){
		Integer couponsid = id;
		Coupons coupons = couponsService.getCoupons(couponsid);
		String integer = memberCouponsService.saveBatchMemberCoupons(coupons, userId);
		return integer;
	}
	
	/**
	 * 一键批量赠送
	 * @return
	 */
	@LogInfo("一键批量赠送")
	@RequestMapping(value="batchGift",method=RequestMethod.GET,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String batchGift(
			@RequestParam(value="telephone",required=false) String telephone,
			@RequestParam(value="orderBeginTime",required=false) String orderBeginTime,
			@RequestParam(value="orderEndTime",required=false) String orderEndTime,
			@RequestParam(value="orderStartAmount",required=false) BigDecimal orderStartAmount,
			@RequestParam(value="orderEndAmount",required=false) BigDecimal orderEndAmount,
			@RequestParam("couponId") Integer couponId,
			@RequestParam("flag") Integer flag){
		
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("telephone", telephone);
		cons.put("orderBeginTime", orderBeginTime);
		cons.put("orderEndTime", orderEndTime);
		cons.put("orderStartAmount", orderStartAmount);
		cons.put("orderEndAmount", orderEndAmount);
		PageBean<Member> pageBean = new PageBean<Member>(0, 15, null, cons);
		pageBean.setUrl("MemberCoupons/giftCoupon");
		List<String> userIdsList = null;
		if (flag.equals(1)) {
			userIdsList = memberService.getAllMemberUserIdByRightJoin(pageBean);
		} else {
			userIdsList = memberService.getAllMemberUserId(pageBean);
		}
		Integer counts = memberService.queryAllowGiftMemberCount(pageBean);
		 String[] arrayUserIds = (String[])userIdsList.toArray(new String[userIdsList.size()]); 
	

		Coupons coupons = couponsService.getCoupons(couponId);
		String integer = memberCouponsService.saveBatchMemberCoupons(coupons, arrayUserIds);
		if ("1".equals(integer)) {
			return counts.toString();
		}
		return "0";
	}
	/**
	 * 删除用户优惠券
	 * @return
	 */
	@LogInfo("删除用户优惠券")
	@RequestMapping("deleteMemberCoupons/{id}")
	public String deleteMemberCoupons(@PathVariable("id") Integer id){
		memberCouponsService.deleteMemberCoupons(id);
		return "redirect:/MemberCoupons/queryMemberCoupon";
	}
	
	/**
	 * 批量删除优惠券
	 * @param ids
	 * @return
	 */
	@LogInfo("批量删除优惠券")
	@ResponseBody
	@RequestMapping(value="batcDelete",method=RequestMethod.POST,produces = "text/html;charset=UTF-8")
	public String deleteByParamIds(@RequestParam(value = "ids[]", required = false) String[] ids){
		if (ids!=null && ids.length>0) {
			try {
				if(!memberCouponsService.batchdeleteMemberCoupons(ids))
					throw new Exception();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				return "0";
			}
		}
		return "1";
	}
}
